package com.rongxue.controller.payment;

import com.rongxue.mapper.SBankTypeMapper;
import com.rongxue.mapper.SUserAccountMapper;
import com.rongxue.mapper.SUserBankMapper;
import com.rongxue.mapper.SUserMapper;
import com.rongxue.pojo.*;
import com.rongxue.service.BankBusinessService;
import com.rongxue.utils.BankCardUtils;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.IDCardUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;


@Controller
public class UserBankCardController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBankCardController.class);

    @Autowired
    private BankBusinessService bankBusinessService;

    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @Autowired
    private SUserBankMapper sUserBankMapper;

    @Autowired
    private SBankTypeMapper sBankTypeMapper;
    @Autowired
    private SUserMapper sUserMapper;
    @Autowired
    private SUserAccountMapper sUserAccountMapper;

//    @ResponseBody
//    @RequestMapping(value = "/tender/binding_bank_no",method =RequestMethod.POST,produces = "application/json")
//    public ObjectResult bindingBankNo(XMLBean xmlBean) throws UnsupportedEncodingException {
//        if (xmlBean.getBank_no() == null || xmlBean.getBank_no().equals("") || xmlBean.getCard_no() == null || xmlBean.getCard_no().equals("") || xmlBean.getReal_name() == null
//                || xmlBean.getReal_name().equals("") || xmlBean.getMobile() == null || xmlBean.getMobile().equals("")){
//            return ObjectResult.build("0", "参数丢失,请重试");
//        }
//
//        if (xmlBean.getBank_no().length() < 16 || xmlBean.getBank_no().length() > 21){
//
//            return ObjectResult.build("0", "银行卡位数错误");
//
//        }else if (!BankCardUtils.checkBankCard(xmlBean.getBank_no())){
//
//            return ObjectResult.build("0", "银行卡错误");
//
//        } else if (!IDCardUtils.cardCodeVerifySimple(xmlBean.getCard_no())){
//
//            return ObjectResult.build("0", "身份证位数错误");
//
//        }else if (!IDCardUtils.cardCodeVerify(xmlBean.getCard_no())){
//
//            return ObjectResult.build("0", "身份证错误");
//
//        } else if (xmlBean.getMobile().length() != 11){
//
//            return ObjectResult.build("0", "手机号位数错误");
//
//        }else if (!xmlBean.getMobile().substring(0,1).equals("1") || xmlBean.getMobile().substring(0,2).equals("14")){
//
//            return ObjectResult.build("0", "手机号错误");
//        }
//        ObjectResult objectResult = bankBusinessService.bindingBankNo(xmlBean);
//
//        return objectResult;
//    }


    /**
     * 获取个人银行卡列表
     *
     * @param xmlBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tender/account_info")
    public ObjectResult selectUserBank(XMLBean xmlBean) {
        if (xmlBean.getToken() == null || xmlBean.getToken().equals("")) return ObjectResult.build("0", "token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUserBank banks = sUserBankMapper.selectUserBankByListUserId(userId);
        //修改汇付code为富友code
//        if(banks.getBankCode().length()<10){
//            SBankType sBankType = sBankTypeMapper.selectByBankFullName("%"+banks.getBankName()+"%");
//            banks.setBankCode(sBankType.getBank_code());
//            sUserBankMapper.updateByPrimaryKeySelective(banks);
//        }
        SUserAccount sUserAccount = sUserAccountMapper.selectUserAccountByUid(Long.parseLong(userId));
        banks.setAccountAble(sUserAccount.getAccount_able());
        banks.setMinMoney("1");
        return ObjectResult.build("1", "成功", banks);
    }

    /**
     * 获取本系统支持的银行列表
     *
     * @param xmlBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/public/bank_list")
    public ObjectResult selectBankList(XMLBean xmlBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        SBankType sBankType = new SBankType();
        List<SBankType> banks = sBankTypeMapper.selectBankList(sBankType);
        map.put("list", banks);
        return ObjectResult.build("1", "成功", map);
    }

    /**
     * 是否实名认证和绑卡
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/teder/realName")
    @ResponseBody
    public ObjectResult realName(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        SUser sUser = sUserMapper.selectUserByMap(map);
        map.clear();
        map.put("card_no_auth",sUser.getRealNameAuth());
        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if(sUserBank==null){
            map.put("is_bank",0);
        }
        else if(sUserBank.getBankCardNo()==null ||"".equals(sUserBank.getBankCardNo())){
            map.put("is_bank",0);
        }else {
            map.put("is_bank",1);
        }
        return ObjectResult.build("1","成功",map);
    }
}





